//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Q4Tech.Engineering.UpdaterAgent.DeviceIdProvider
{
    internal class HardDeviceIdProvider : IDeviceIdProvider
	{
		[DllImport("coredll.dll")]
		private extern static int GetDeviceUniqueID([In, Out] byte[] appdata,
													int cbApplictionData,
													int dwDeviceIDVersion,
													[In, Out] byte[] deviceIDOuput,
													out uint pcbDeviceIDOutput);

		public string GetDeviceId()
		{
			byte[] appBytes = System.Text.Encoding.Unicode.GetBytes("Updater");
			byte[] deviceId = new byte[20];
			uint size = 20;
			GetDeviceUniqueID(appBytes, appBytes.Length, 1, deviceId, out size);

			// TODO: we're ignoring the last 4 bytes on the returned array.
			byte[] deviceGuid = new byte[16];
			Array.Copy(deviceId, deviceGuid, 16);

			return new Guid(deviceGuid).ToString();
		}
	}
}
